package com.pureut.storage.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pureut.storage.domain.MiscellaneousReceiptDetail;
import com.pureut.storage.dto.MiscellaneousReceiptDetailDto;
import com.pureut.storage.dto.MiscellaneousReceiptDto;
import com.pureut.storage.mapper.MiscellaneousReceiptDetailMapper;
import com.pureut.storage.mapper.MiscellaneousReceiptMapper;
import com.pureut.storage.service.MiscellaneousReceiptDetailService;
import com.sale.common.core.constant.CacheNames;
import com.sale.common.dict.utils.DictUtils;
import com.sale.common.mybatis.core.page.PageQuery;
import com.sale.common.mybatis.core.page.TableDataInfo;
import com.sale.common.redis.utils.CacheUtils;
import com.sale.system.api.RemoteUnitService;
import com.sale.system.api.domain.SysAuxiliary;
import com.sale.system.api.domain.SysDictData;
import com.sale.system.api.model.MaterialUnitDto;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/***
 * 其他入库明细实现层
 * Author:C
 * Date:2023/01/19 13:53
 * @DESC
 */
@Service
public class MiscellaneousReceiptDetailServiceImpl extends ServiceImpl<MiscellaneousReceiptDetailMapper, MiscellaneousReceiptDetail> implements MiscellaneousReceiptDetailService {

    @Resource
    MiscellaneousReceiptDetailMapper miscellaneousReceiptDetailMapper;

    @Resource
    MiscellaneousReceiptMapper miscellaneousReceiptMapper;

    @DubboReference
    RemoteUnitService remoteUnitService;

    /**
     * 其它入库明细列表
     * @param id
     * @return
     */
    @Override
    public TableDataInfo<MiscellaneousReceiptDetailDto> getMiscellaneousReceiptDetail(Long id, PageQuery pageQuery) {
        Page<MiscellaneousReceiptDetailDto> detailList = miscellaneousReceiptDetailMapper.getDetailList(id,pageQuery.build());
        //单位集合
        Map<String, MaterialUnitDto> unitByCodeToMap = remoteUnitService.getUnitByCodeToMap();
        MiscellaneousReceiptDto miscellaneousById = miscellaneousReceiptMapper.getMiscellaneousById(id);
//        //获取网型标准缓存字典数据
//        List<SysDictData> netArray = DictUtils.getDictCache("sys_of_type");
//        Map<String, String> netMap = netArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));
//        //获取铺网类型标准缓存字典数据
//        List<SysDictData> netTypeArray = DictUtils.getDictCache("sys_type_net");
//        Map<String, String> netTypeMap = netTypeArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));
        //明细状态
        List<SysDictData> detailArray = DictUtils.getDictCache("miscellaneous_receipt_detail_status");
        Map<String, String> statusMap = detailArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));
        List<MiscellaneousReceiptDetailDto> records = detailList.getRecords();
        for (MiscellaneousReceiptDetailDto entity : records) {
            entity.setStatusDict(statusMap.get(entity.getStatus()));
            //物料单位
            if (entity.getMaterialUnit() != null) {
                entity.setUnitDict(unitByCodeToMap.get(entity.getMaterialUnit()).getUnitName());
            }
            //辅助属性
            List<SysAuxiliary> statsArray = CacheUtils.get(CacheNames.SYS_GINSENG + "_" + miscellaneousById.getTenantId(), String.valueOf(miscellaneousById.getMaterialId()));
            entity.setAuxiliaryAttribute(statsArray);
//            entity.setMaterialNetDict(netMap.get(entity.getMaterialNet()));
//            entity.setMaterialNetTypeDict(netTypeMap.get(entity.getMaterialNetType()));
        }
        return TableDataInfo.build(detailList);
    }
}
